package demo.spring.boot.redis;

import demo.spring.boot.jdbc.entity.Department;
import demo.spring.boot.jdbc.entity.Role;
import demo.spring.boot.jdbc.entity.User;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lushun.chen on 2017/3/13.
 *
 * @author lushun.chen
 */
@RestController
@RequestMapping("/")
public class MyController {
    private static Logger logger = LoggerFactory.getLogger(MyController.class);
    @Autowired
    private MyRedis myRedis;
    @Autowired
    private UserRedis userRedis;


    @RequestMapping("/")
    public Object demo() {
        System.out.println("1234567890----");
        boolean b = myRedis.hasData();
        return b;
    }

    @RequestMapping("/1")
    public Object demo1() {
        return userRedis.get("");
    }

    @RequestMapping("/2")
    public Object demo2() {
        User user = new User();
        user.setName("张三");
        user.setCreateDate(new Date());
        userRedis.add("user", 1234567890L, user);
        return user;
    }

    @RequestMapping("/3")
    public Object demo3() {
        System.out.println("1234567890----");
        boolean b = myRedis.hasData();
        return b;
    }

    @RequestMapping("/4")
    public Object demo4() {
        System.out.println("1234567890----");
        boolean b = myRedis.hasData();
        return b;
    }

    @PostConstruct
    public void init() {

        Department department = new Department();
        department.setName("省委宣传部");

        Role role = new Role();
        role.setName("省委书记");

        User user = new User();
        user.setName("李达康");
        user.setCreateDate(new Date());
        user.setDepartment(department);

        List<Role> roleList = new ArrayList<>();
        roleList.add(role);
        user.setRoles(roleList);

        userRedis.delete(this.getClass().getName() + "userByName:" + user.getName());
        userRedis.add(this.getClass().getName() + "userByName:" + user.getName(), 10L, user);

        User user1 = userRedis.get(this.getClass().getName() + "userByName:" + user.getName());

        Assert.assertNotNull(user1);

        logger.info("=====user======name:{},department:{},role:{},",
                user1.getName(), user.getDepartment().getName(),
                user.getRoles().get(0).getName());
    }
}
